package com.back.service.impl;

import cn.hutool.core.util.StrUtil;
import com.back.exception.BusinessException;
import com.back.exception.ErrorCode;
import com.back.mapper.WeaponMapper;
import com.back.model.entity.Weapon;
import com.back.service.WeaponService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 李瑞
* @description 针对表【Weapon】的数据库操作Service实现
* @createDate 2025-09-12 19:07:44
*/
@Service
@Slf4j
public class WeaponServiceImpl extends ServiceImpl<WeaponMapper, Weapon>
    implements WeaponService{

    @Resource
    private WeaponMapper weaponMapper;

    @Override
    public IPage<Weapon> listWeaponByPage(Page<Weapon> page, String weaponName, Integer heroId) {
        return weaponMapper.listWeaponByPage(page, weaponName, heroId);
    }

    @Override
    public List<Weapon> getWeaponsByHeroId(Integer heroId) {
        if (heroId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "英雄ID不能为空");
        }
        return weaponMapper.getWeaponsByHeroId(heroId);
    }

    @Override
    public List<Weapon> getAllWeapons() {
        return weaponMapper.getAllWeapons();
    }

    @Override
    public boolean createWeapon(Weapon weapon) {
        // 校验武器数据
        validWeapon(weapon, true);
        
        // 检查武器名称是否重复
        QueryWrapper<Weapon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("weaponName", weapon.getWeaponName());
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器名称已存在");
        }
        
        return this.save(weapon);
    }

    @Override
    public boolean updateWeapon(Weapon weapon) {
        // 校验武器数据
        validWeapon(weapon, false);
        
        // 检查武器是否存在
        Weapon existWeapon = this.getById(weapon.getWeaponId());
        if (existWeapon == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "武器不存在");
        }
        
        // 检查武器名称是否重复（排除自己）
        QueryWrapper<Weapon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("weaponName", weapon.getWeaponName());
        queryWrapper.ne("weaponId", weapon.getWeaponId());
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器名称已存在");
        }
        
        return this.updateById(weapon);
    }

    @Override
    public boolean deleteWeapon(Integer weaponId) {
        if (weaponId == null || weaponId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器ID不能为空");
        }
        
        // 检查武器是否存在
        Weapon weapon = this.getById(weaponId);
        if (weapon == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "武器不存在");
        }
        
        return this.removeById(weaponId);
    }

    @Override
    public Weapon getWeaponById(Integer weaponId) {
        if (weaponId == null || weaponId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器ID不能为空");
        }
        
        Weapon weapon = this.getById(weaponId);
        if (weapon == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "武器不存在");
        }
        
        return weapon;
    }

    @Override
    public void validWeapon(Weapon weapon, boolean add) {
        if (weapon == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器信息不能为空");
        }
        
        String weaponName = weapon.getWeaponName();
        Integer attack = weapon.getAttack();
        
        // 创建时，参数不能为空
        if (add) {
            if (StrUtil.isBlank(weaponName)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器名称不能为空");
            }
            if (attack == null) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器攻击力不能为空");
            }
        }
        
        // 有参数则校验
        if (StrUtil.isNotBlank(weaponName) && weaponName.length() > 255) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器名称过长");
        }
        
        if (attack != null && attack < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器攻击力不能为负数");
        }
        
        Double critRate = weapon.getCritRate();
        if (critRate != null && (critRate < 0 || critRate > 1)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "武器暴击率必须在0-1之间");
        }
    }

}




